<?php
namespace app\index\controller;

use app\index\model\Article;
use app\index\model\Collect;
use app\index\model\Comment;
use app\index\model\Group;
use app\index\model\User;

use think\Controller;
use think\Request;

class Index extends  Controller
{
    public function _initialize() {
        if(!session('?username')) {
            $this->error('您还未登陆，请先登录','index/Login/login');
        }
    }
    /**
     * @展示后台首页
     */
    public function index()
    {
        return $this->fetch();
    }

//    public function index1() {
//        $res = new Admin();
//        $r = $res->index();
//        return json_encode($r);
//    }

    /**
     * 后台用户管理
     */
    public function user()
    {
        $user = new User();
        $res = $user->sel();
        $data = [];
        foreach ($res as $val) {
            array_push($data, $val->toArray());
        }
        $this->assign('data', $data);
        return $this->fetch();
    }

    //删除用户
    public function userdel($id)
    {
        $user = new User();
        $res = $user->del($id);
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error("删除失败");
        }
    }
    //收藏管理
    public function collect($user_id) {
        $col = new Collect();
        $res = $col->sel($user_id);
        $this->assign('data',$res);
        return $this->fetch();
    }

    /**
     * 分类管理
     */
    public function group()
    {
        $class = new Group();
        $res = $class->sel();
        $res = collection($res)->toArray();
        $this->assign('data', $res);
        return $this->fetch();
    }

    //添加分类
    public function groupAdd(Request $request)
    {
        $name = $request->post('name');
        if ($request->isPost() && !empty($name)) {
            $class = new Group();
            $res = $class->add($name);
            if ($res) {
                $this->success('新增成功', 'index/group');
            } else {
                $this->error('新增失败');
            }
        } else {
            return $this->fetch();
        }
    }

    //删除分类
    public function groupDel($id)
    {
        $class = new Group();
        $res = $class->del($id);
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 文章管理
     */

    public function article()  {
        $art = new Article();
        $res = $art->sel();
        $res = collection($res)->toArray();
        $this->assign('data',$res);
        return $this->fetch();
    }
    //添加文章
    public function articleAdd(Request $request)
    {
        $data = $request->post();
//        dump($data);
//        exit;
        if($request->isPost() && !empty($data)) {
            $art = new Article();
            $res = $art->add($data);
            if($res) {
                $this->success('添加成功');
            }else {
                $this->error('添加失败');
            }
        }else {
            $class = new Group();
            $res = $class->sel();
            $res = collection($res)->toArray();
            $this->assign('data', $res);
            return $this->fetch();
        }
    }
    //删除文章
    public function articleDel($id) {
        $art = new Article();
        $res = $art->del($id);
        if($res) {
            $this->success('删除成功');
        }else {
            $this->error('删除失败');
        }
    }

    //图片上传
    public function upload(Request $request)
    {
//thinkphp5的框架，如果是原生的，用$_FiLES['file']获取到图片，
        $file 	= $request->file('file');
        $info 	= $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            $name_path =str_replace('\\',"/",$info->getSaveName());
            $result['data']["src"] = "/uploads/layui/".$name_path;
            $url 	= $info->getSaveName();
            //图片上传成功后，组好json格式，返回给前端
            $arr   = array(
                'code' => 0,
                'message'=>'',
                'data' =>array(
                    'src' => "/uploads/".$name_path
                ),
            );
        }

        echo json_encode($arr);

    }

    /**
     * 评论管理
     */
    public function comment() {
        $com = new Comment();
        $res = $com->sel();
//        dump($res);
//        exit;
//        $res = collection($res)->toArray();
        $this->assign('data',$res);
        return $this->fetch();
    }
    //删除评论
    public function commentDel($id) {
        $com = new Comment();
        $res = $com->del($id);
        if($res) {
            $this->success('删除成功');
        }else {
            $this->error('删除失败');
        }
    }
}
